import { http } from '../utils/http'

/**
 * 请假登记文件接口
 */
export interface RegisterFileVO {
    leaveRegisterId: number // 关联LEAVE_REGISTER
    leaveFileUrl: string // 证明材料路径
    leaveFileName: string // 证明材料名称
    leaveFileSize: number // 证明材料大小
}

/**
 * 请假登记文件更新接口
 */
export interface RegisterFileUpdateVO {
    id: number // 附件记录ID
    leaveRegisterId: number // 关联LEAVE_REGISTER
    leaveFileUrl: string // 证明材料路径
    leaveFileName: string // 证明材料名称
    leaveFileSize: number // 证明材料大小
}

/**
 * 请假登记接口
 */
export interface RegisterVO {
    id?: number
    nickName?: string
    deptName?: string
    startDate?: number | string  
    endDate?: number | string    
    status?: number
    reasons?: string
    processInstanceId?: string
    personId?: number
    deptId?: number
    destination?: string
    hostArrangement?: string
    remark?: string
    leaderOpinion?: string
    hostId?: string  
    personAdmitId?: string
    createTime?: number | string  
    cancelForm?: {
        status: number
        processInstanceId?: string
    }
}

// 目的地数据接口
export interface DestinationData {
    locationType: 'domestic' | 'foreign'
    destination: string
    foreignAddress: string
}

// 调研数据接口
export interface ResearchData {
    startDate: string
    endDate: string
    theme: string
    purpose: string
    organization: string
}

// 培训数据接口
export interface TrainingData {
    startDate: string
    endDate: string
    theme: string
    organization: string
}

// 公务数据接口
export interface BusinessData {
    startDate: string
    endDate: string
    meetingName: string
    organization: string
}

// 因私数据接口
export interface PersonalData {
    startDate: string
    endDate: string
    type: string
    details: string
    phone: string
    // 探亲假相关字段
    route?: string
    relation?: string
    visitName?: string
    visitType?: string
    visitAddress?: string
    maritalStatus?: string
    // 生育相关字段
    parity?: string
    multiple?: string
    dystocia?: string
}

// 学术会议数据接口
export interface AcademicData {
    startDate: string
    endDate: string
    meetingName: string
    organizer: string
    nature: string
    hasReport: string
    reportTitle?: string
    academicPaperCount: number
}

// 其他事由数据接口
export interface OtherData {
    description: string
}

// 请假事由详细信息接口
export interface ReasonFormData {
    id: string
    leaveRegisterId: number
    type: number
    subject?: string
    purpose?: string
    unit?: string
    startDate?: string
    endDate?: string
    privateType?: string
    detail?: string
    phone?: string
    visitRoute?: string
    relationship?: string
    visitedPersonName?: string
    visitType?: string
    registeredAddress?: string
    maritalStatus?: string
    birthCount?: string
    multipleBirth?: string
    difficultBirth?: string
    // 学术会议相关字段
    meetingNature?: number
    reportType?: string
    reportTitle?: string
    reportPaperCount?: number
    hasReport?: string
}

// 请假表单数据接口
export interface LeaveFormData {
    startDate: string
    endDate: string
    selectedReasons: (string | number)[]
    destinations: DestinationData[]
    workArrangement: string
    remark: string
    nextApprovers: { [key: string]: string[] }
    // 详细事由字段
    researchItems: ResearchData[]
    trainingItems: TrainingData[]
    businessItems: BusinessData[]
    personalItems: PersonalData[]
    academicItems: AcademicData[]
    otherItems: OtherData[]
}

// 日期选择器状态接口
export interface DatePickerStates {
    [key: number]: boolean
}

/**
 * 分页查询参数
 */
export interface PageParams {
    pageNo?: number
    pageSize?: number
    personId?: number
    deptId?: number
    startDate?: string
    endDate?: string
    destination?: string
    hostArrangement?: string
    remark?: string
    leaderOpinion?: string
    hostId?: number
    status?: number
    personAdmitId?: number
    createTime?: string[]
    isBackLeave?: boolean
}

/**
 * 分页响应接口
 */
export interface PageResult<T> {
    list: T[]
    total: number
}

/**
 * API 响应包装接口
 */
export interface ApiResponse<T = any> {
    code: number
    data: T
    msg?: string
}

/**
 * 请假登记 API
 */
export const RegisterApi = {
    /**
     * 查询请假登记分页
     * @param params 查询参数
     * @returns 分页结果
     */
    getRegisterPage: (params: PageParams): Promise<ApiResponse<PageResult<RegisterVO>>> => {
        return http.get('/leave/register/page', params)
    },

    /**
     * 查询请假待办分页
     * @param params 查询参数
     * @returns 分页结果
     */
    getRegisterTodoPage: (params: PageParams): Promise<PageResult<RegisterVO>> => {
        return http.get('/leave/register/todo-page', params)
    },

    /**
     * 查询请假已办分页
     * @param params 查询参数
     * @returns 分页结果
     */
    getRegisterDonePage: (params: PageParams): Promise<PageResult<RegisterVO>> => {
        return http.get('/leave/register/done-page', params)
    },

    /**
     * 查询请假登记详情
     * @param id 请假登记ID
     * @returns 请假登记详情
     */
    getRegister: (id: number): Promise<ApiResponse<RegisterVO>> => {
        return http.get(`/leave/register/get?id=${id}`)
    },

    /**
     * 根据流程实例ID获取请假登记ID
     * @param processInstanceId 流程实例ID
     * @returns 请假登记ID
     */
    getRegisterId: (processInstanceId: string): Promise<number> => {
        return http.get(`/leave/register/getId?processInstanceId=${processInstanceId}`)
    },

    /**
     * 新增请假登记
     * @param data 请假登记数据
     * @returns 创建结果
     */
    createRegister: (data: RegisterVO): Promise<any> => {
        return http.post('/leave/register/create', data)
    },

    /**
     * 修改请假登记
     * @param data 请假登记数据
     * @returns 更新结果
     */
    updateRegister: (data: RegisterVO): Promise<any> => {
        return http.put('/leave/register/update', data)
    },

    /**
     * 删除请假登记
     * @param id 请假登记ID
     * @returns 删除结果
     */
    deleteRegister: (id: number): Promise<any> => {
        return http.delete(`/leave/register/delete?id=${id}`)
    },

    /**
     * 导出请假登记 Excel
     * @param params 导出参数
     * @returns 导出结果
     */
    exportRegister: (params: PageParams): Promise<any> => {
        return http.get('/leave/register/export-excel', { params })
    },
}

/**
 * 请假理由附件 API
 */
export const RegisterFileApi = {
    /**
     * 查询请假理由附件分页
     * @param params 查询参数
     * @returns 分页结果
     */
    getRegisterFilePage: (params: PageParams): Promise<PageResult<RegisterFileVO>> => {
        return http.get('/leave/register-file/page', { params })
    },

    /**
     * 查询请假理由附件详情
     * @param id 请假登记ID
     * @returns 附件列表
     */
    getRegisterFile: (id: number): Promise<ApiResponse<RegisterFileVO[]>> => {
        return http.get(`/leave/register-file/getlist?registerId=${id}`)
    },

    /**
     * 新增请假理由附件
     * @param data 附件数据
     * @returns 创建结果
     */
    createRegisterFile: (data: RegisterFileVO): Promise<any> => {
        return http.post('/leave/register-file/create', data)
    },

    /**
     * 修改请假理由附件
     * @param data 附件数据
     * @returns 更新结果
     */
    updateRegisterFile: (data: RegisterFileUpdateVO): Promise<any> => {
        return http.put('/leave/register-file/update', data)
    },

    /**
     * 删除请假理由附件
     * @param id 附件ID
     * @returns 删除结果
     */
    deleteRegisterFile: (id: number): Promise<any> => {
        return http.delete(`/leave/register-file/delete?id=${id}`)
    },

    /**
     * 导出请假理由附件 Excel
     * @param params 导出参数
     * @returns 导出结果
     */
    exportRegisterFile: (params: PageParams): Promise<any> => {
        return http.get('/leave/register-file/export-excel', { params })
    },
}

/**
 * 请假理由表单 API
 */
export const ReasonFormApi = {
    /**
     * 新增请假理由表单
     * @param data 请假理由表单数据
     * @returns 创建结果
     */
    createReasonForm: (data: any): Promise<ApiResponse<any>> => {
        return http.post('/leave/reason-form/create', data)
    },

    /**
     * 修改请假理由表单
     * @param data 请假理由表单数据
     * @returns 修改结果
     */
    updateReasonForm: (data: any): Promise<ApiResponse<any>> => {
        return http.put('/leave/reason-form/update', data)
    },

    /**
     * 删除请假理由表单
     * @param id 请假理由表单ID
     * @returns 删除结果
     */
    deleteReasonForm: (id: number): Promise<ApiResponse<any>> => {
        return http.delete(`/leave/reason-form/delete?id=${id}`)
    },

    /**
     * 获取请假理由表单列表
     * @param registerId 请假登记ID
     * @returns 请假理由表单列表
     */
    getReasonFormList: (registerId: number): Promise<ApiResponse<ReasonFormData[]>> => {
        return http.get(`/leave/reason-form/getlist?registerId=${registerId}`)
    },
}

/**
 * 销假 API
 */
export const CancelFormApi = {
    /**
     * 新增销假流程
     * @param params 销假参数
     * @returns 创建结果
     */
    createCancelForm: (params: any): Promise<any> => {
        return http.get('/leave/app-cancel-form/create', { params })
    },

    /**
     * 查询销假表单ID
     * @param processInstanceId 流程实例ID
     * @returns 请假登记ID
     */
    getCancelFormId: (processInstanceId: string): Promise<number> => {
        return http.get('/leave/app-cancel-form/get-leaveRegisterId', {
            params: { processInstanceId }
        })
    },
}

/**
 * 请假状态枚举
 */
export enum LeaveStatus {
    REJECTED = 0,    // 未通过
    PENDING = 1,     // 待会签
    APPROVING = 2,   // 待审批
    COMPLETED = 3,   // 已完成
    CANCELLED = 4,   // 已取消
}

/**
 * 销假状态枚举
 */
export enum CancelStatus {
    PENDING_APPROVAL = 0,  // 待审批
    PENDING_SIGN = 1,      // 待会签
    CANCELLED = 2,         // 已销假
    PENDING_CANCEL = 3,    // 待销假
}



